package Library;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.BoxLayout;

//import com.sun.org.apache.xerces.internal.impl.xs.XSConstraints;
/**
 * This class is the histogram controller
 */
public class Histogram extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3049995314469783566L;
	/**
	 * table
	 */
	private JTable table;
	/**
	 * values to display as histogram
	 */
	public int values[];	
	/**
	 * color matrix for each cell
	 */
	public static Color[][] cellColors;
	/**
	 * number of rows
	 */
	public int histogramHeight;	
	/**
	 * column width
	 */
	private int maxColumnWidth;	
	/**
	 * panel
	 */
	private JPanel southPanel;//panel
	
	private String[] columnHeader;
	
	/**
	 * 
	 * @param columnHeader
	 * @param avalues
	 * @param ahistogramHeight
	 * @param amaxColumnWidth
	 */
	public Histogram(String[] columnHeader, int[] avalues, int ahistogramHeight, int amaxColumnWidth) {
		super();//JPanel constructor
		
		setLayout(new BorderLayout(0, 0));
		
		this.values = avalues;
		this.histogramHeight = ahistogramHeight;
		this.maxColumnWidth = amaxColumnWidth;
		this.columnHeader = columnHeader;
		
		/*input checks*/
	}
	public int his ()
	{
		int i,j;
		if (values==null){
			System.err.println("no values entered");
			return 0;
		}
		if (histogramHeight<=1){
			System.err.println("row height can not be smaller that 2");
			return 0;
		}
		if (maxColumnWidth<=0){
			System.err.println("Column Size can not be negative value");
			return 0;
		}
			
		/*column colors*/
		Color[] colorPallet = {new Color(147, 137, 83), new Color(149, 175, 215), new Color(217, 150, 148),new Color(191, 191, 191), new Color(195, 214, 155), new Color(178, 162, 199), new Color(146, 205, 220),new Color(250, 192, 143),new Color(84, 141, 212)};

		table = new JTable(); //new table
		table.setEnabled(false);	//table parameters
		table.setShowVerticalLines(false);
		table.setRowSelectionAllowed(false);
		table.setFillsViewportHeight(true);
	    table.getTableHeader().setReorderingAllowed(false);
	    table.getTableHeader().setResizingAllowed(true);

		Object obj[][] = new Object[histogramHeight][values.length];//initialize table model values matrix
		for (int i1=0; i1<histogramHeight; i1++)
			for (int j1=0; j1<values.length; j1++)
				obj[i1][j1] = null;	    

		table.setModel(new DefaultTableModel(obj, columnHeader));//add table model to table
		
		/*-----  setting cell colors -----*/
		cellColors = new Color[this.values.length][histogramHeight];//define size of cellColors matrix
		
		
		
		/*set each cells color*/
		for (i=0; i<this.values.length; i++){
			boolean flag = true;
			for (j=0; j<histogramHeight; j++){ //number of rows is dynamic
				float val = values[i];
				val = val/100;
				val = val*10;
				if (val >= (float)j){
					cellColors[i][table.getRowCount()-j-1] = colorPallet[i%9];
					if (j==histogramHeight-1)
						table.getModel().setValueAt(Integer.toString(values[i]/10), 0, i);						
				}
				else{
					if (flag){
						table.getModel().setValueAt(Integer.toString(values[i]/10), Math.max(0, histogramHeight-j), i);
						flag=false;
					}
					cellColors[i][table.getRowCount()-j-1] = Color.WHITE;
				}//else
			}//end for
		}//end for
		
		//set cell renderrer for each cell by columnModel
	    TableColumn tcol;
	    for (i=0; i<table.getColumnCount();i++){
	    	tcol = table.getColumnModel().getColumn(i);
	    	tcol.setCellRenderer(new TableH(this.cellColors));
	    	tcol.setMaxWidth(maxColumnWidth);
	    }		
		
		southPanel = new JPanel();
		southPanel.setBackground(Color.WHITE);
		southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.PAGE_AXIS));
		add(southPanel, BorderLayout.SOUTH);
			
		southPanel.add(table);
		southPanel.add(table.getTableHeader());
		SHOW();
		return 1;
	}
	public void SHOW()
	{
        JFrame frame = new JFrame("Here are the results");
        this.setSize(600,600);
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JScrollPane tableContainer = new JScrollPane(table);

        panel.add(tableContainer, BorderLayout.CENTER);
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
	}
}

